package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理接口")
public class SetMealController {
    @Autowired
    SetMealService setMealService;

    /**
     * 套餐管理的分页查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐管理的分页查询")
    public Result<PageResult> selectPage(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐管理的分页查询开始...");
        PageResult pageResult = setMealService.selectPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setMeal",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("{}套餐新增中...",setmealDTO.getName());
        setMealService.save(setmealDTO);
        return  Result.success();
    }

    /**
     * 用于修改操作的回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据套餐id查询套餐")
    public Result<SetmealVO> selectById(@PathVariable Long id){
        log.info("分类id为{}的套餐查找中...",id);
        SetmealVO s = setMealService.selectById(id);
        return  Result.success(s);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setMeal",allEntries = true)
    public Result edi(@RequestBody SetmealDTO setmealDTO){
        log.info("{}套餐信息修改中...",setmealDTO.getName());
        setMealService.edi(setmealDTO);
        return Result.success();
    }

    /**
     * 起售停售套餐
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启售停售")
    @CacheEvict(cacheNames = "setMeal",allEntries = true)
    public Result startOrBin(@PathVariable Integer status,Long id){
        log.info("套餐的状态改为：{}，{}",status == 1 ? "启售": "停售",id);
        setMealService.startOrBin(status,id);
        return Result.success();
    }

    /**
     * 套餐的删除
     * @return
     */
    @DeleteMapping
    @ApiOperation("套餐的删除")
    @CacheEvict(cacheNames = "setMeal",allEntries = true)
    public Result delete(@RequestParam List<Long> ids ){// 使用@RequestParam进行处理字符串
        log.info("套餐的批量删除中...");
        setMealService.delete(ids);
        return Result.success();
    }
}
